package com.sanqing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sanqing.dao.base.BaseDao;
import com.sanqing.po.SaleAcivity;
import com.sanqing.service.SaleService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;
import com.sanqing.util.PageUtil;
import com.sanqing.util.TimeHelper;

@Service("SaleService")
@Transactional(readOnly = true)
public class SaleServiceImpl implements SaleService {

	@Resource(name = "saleDao")
	private BaseDao<SaleAcivity, String> saleDao;

	@Override
	public PageResult querySaleByPage(Page page, SaleAcivity Sale) {
		Map<String, Object> map = new HashMap<String, Object>();
		String hql = "from SaleAcivity a where 1=1";

		page = PageUtil.createPage(page.getEveryPage(), (int) saleDao.countHqlResult(hql, map), page.getCurrentPage());// 根据总记录数创建分页信息
		List<SaleAcivity> list = saleDao.findByPage(page, hql, map);// 通过分页信息取得试题
		PageResult result = new PageResult(page, list);// 封装分页信息和记录信息，返回给调用处
		return result;
	}

	@Override
	@Transactional(readOnly = false)
	public void add(SaleAcivity sale) {
		sale.setCreateTime(TimeHelper.getCurrentTime());
		saleDao.save(sale);
	}

	@Override
	public SaleAcivity getById(String id) {
		return saleDao.getByKey(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void edit(SaleAcivity Sale) {
		SaleAcivity r = saleDao.getByKey(Sale.getId());
		BeanUtils.copyProperties(Sale, r,new String[]{"createTime"});
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		saleDao.delete(id);
	}

}
